<?php
/**
 * @copyright meila.com
 * @author wangtao5@
 * @name
 * @param
 */

class ml_model_album_rdsAlbum extends ml_model_redis
{
    const PFX_A_FEED_USERS = 'aFdUrs_';
    const PFX_A_REPLY_USERS = 'aRyUrs_';
    const HASH_A_BY_TITLE = 'hashAByTie';
    function __construct() {
        if(!$this->init_rds('meila_album'))
            return false;
    }
    /**
     * 根据album_id获取画报讨论的用户
     */
    public function get_album_users($album_id)
    {
        $key = self::PFX_A_FEED_USERS.$album_id;
        $end = ML_ALBUM_ALBUMFEED_USERS-1;
        $users = $this->zRange($key, 0, $end);
        return $users;
    }
    /**
     * 添加参与画报讨论的用户
     */
    public function set_album_users($album_id, $uid){
        $key = self::PFX_A_FEED_USERS.$album_id;
        return $this->zAdd($key, time(), $uid);
    }
    
    /**
     * 根据album_id获取画报讨论的用户数
     */
    public function get_users_count($album_id){
        $key = self::PFX_A_FEED_USERS.$album_id;
        return $this->zSize($key);
    }
    /**
     * 根据albumfeed_rid获取分享回复用户
     */
    public function get_album_reply_users($rid)
    {
        $key = self::PFX_A_REPLY_USERS.$rid;
        $end = ML_ALBUM_ALBUMREPLY_USERS-1;
        $users = $this->zRange($key, 0, $end);
        return $users;
    }
    /**
     * 添加分享回复用户
     */
    public function set_album_replay_users($rid, $uid){
        $key = self::PFX_A_REPLY_USERS.$rid;
        return $this->zAdd($key, time(), $uid);
    }
    /**
     * 根据albumfeed_rid获取分享回复用户数
     */
    public function get_album_reply_users_count($album_id){
        $key = self::PFX_A_REPLY_USERS.$album_id;
        return $this->zSize($key);
    }
    
    public function get_album_by_titles($data){
        if(!is_array($data))
            return false;
        $hash_titles = array();
        foreach($data as $k=>$val){
            $hash = ml_tool_resid::str_hash($val);
            $hash_titles[$hash] = $val;
            $fields[] = $hash;
        }
        $key = self::HASH_A_BY_TITLE;
        $rs = $this->hMGet($key, $fields);
        if(!$rs)
            return false;
        
        foreach($rs as $k=>$val){
            if(!$val)
                continue;
            $album = explode(':', $val);
            $titles[$hash_titles[$k]] = array('class_id'=>$album[0], 'album_id'=>$album[1]); 
        }
        return isset($titles) ? $titles : false;        
    }
    public function exists_album_by_titles($data){
        if(!is_array($data))
            return false;
        $hash_titles = array();
        foreach($data as $k=>$val){
            $hash = ml_tool_resid::str_hash($val);
            $hash_titles[$hash] = $val;
            $fields[] = $hash;
        }
        $key = self::HASH_A_BY_TITLE;
        $rs = $this->hMGet($key, $fields);
        if(!$rs)
            return false;
    
        foreach($rs as $k=>$val){
            if(!$val)
                continue;
            $album = explode(':', $val);
            $titles[] = $hash_titles[$k];
        }
        
        return isset($titles) ? $titles : false;
    }
}
?>